package com.lsl.controller;

import com.lsl.entity.Blog;
import com.lsl.entity.Result;
import com.lsl.service.BlogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/blog-service")
public class CommonBlogController {

    @Autowired
    private BlogService blogService;

    @GetMapping("/getBlog/id/{id}")
    public Result<Blog> getBlogById(@PathVariable Integer id) {
        return Result.success(blogService.getById(id));
    }

    @GetMapping("/getBlog/songid/{songid}")
    public Result<List<Blog>> getBlogsBySongId(@PathVariable String songid) {
        return Result.success(blogService.lambdaQuery()
                .eq(Blog::getSongID, songid)
                .list());
    }

    @GetMapping("/getBlog/all")
    public Result<List<Blog>> getAllBlogs() {
        return Result.success(blogService.list());
    }

    @GetMapping("/getBlog/upvotes/{num}")
    public Result<List<Blog>> getBlogsByUpvotes(@PathVariable Integer num) {
        return Result.success(blogService.lambdaQuery()
                .gt(Blog::getUpVotes, num)
                .list());
    }

    @GetMapping("/getBlog/downvotes/{num}")
    public Result<List<Blog>> getBlogsByDownvotes(@PathVariable Integer num) {
        return Result.success(blogService.lambdaQuery()
                .gt(Blog::getDownVotes, num)
                .list());
    }

    @DeleteMapping("/deleteBlog/id/{id}")
    public Result<Boolean> deleteBlogById(@PathVariable Integer id) {
        return Result.success(blogService.removeById(id));
    }
}
